package com.shuai.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shuai.model.Order;
import com.shuai.model.User;
import org.springframework.boot.CommandLineRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author shuaihong-coding
 * @date 2025/10/2
 * @className OrderController
 */
@RestController
public class OrderController implements CommandLineRunner {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Resource
    private RestTemplate restTemplate;

    /**
     * 获取订单信息
     */

    @Override
    public void run(String... args) throws Exception {
        String forObject = restTemplate.getForObject("http://localhost:8000/user/getUserInfo", String.class);
        User user = objectMapper.readValue(forObject, User.class);
        Order order = Order.builder()
                .orderName("订单001")
                .price(5.0)
                .count(1)
                .build();
        order.setUser(user);
        String orderStrJson = objectMapper.writeValueAsString(order);
        System.out.println("orderStrJson ： "+orderStrJson);
    }
}
